//
//  ViewController.m
//  WangI
//
//  Created by wjn on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "ViewController.h"
#import "ScrollModel.h"
#import "NewsModel.h"
#import "NewsTableViewCell.h"
#import "NewsCollectionViewCell.h"



@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIButton *newsButton;
@property (nonatomic, retain) UIButton *amuseButton;
@property (nonatomic, retain) UIButton *peButton;
@property (nonatomic, retain) UIButton *enocomyButton;


@end

@implementation ViewController


- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationItem.title = @"网易新闻";
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title.jpg"]];
    [self getNewsData];
    [self getAmuseData];
    [self getPEData];
    [self getEconomyData];
    [self.view addSubview:self.collectionView];
    
    UIView *pilot = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 35)];
    pilot.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:pilot];
    
    _newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _newsButton.frame = CGRectMake(30, 5, 50, 25);
    [_newsButton setTitle:@"头条" forState:UIControlStateNormal];
    [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton addTarget:self action:@selector(newsButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [pilot addSubview:_newsButton];
    
    _amuseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _amuseButton.frame = CGRectMake(120, 5, 50, 25);
    [_amuseButton setTitle:@"娱乐" forState:UIControlStateNormal];
    [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_amuseButton addTarget:self action:@selector(amuseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [pilot addSubview:_amuseButton];
    
    _peButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _peButton.frame = CGRectMake(210, 5, 50, 25);
    [_peButton setTitle:@"体育" forState:UIControlStateNormal];
    [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_peButton addTarget:self action:@selector(peButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [pilot addSubview:_peButton];
    
    _enocomyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _enocomyButton.frame = CGRectMake(300, 5, 50, 25);
    [_enocomyButton setTitle:@"经济" forState:UIControlStateNormal];
    [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_enocomyButton addTarget:self action:@selector(enocomyButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [pilot addSubview:_enocomyButton];
}

#pragma mark - buttonAction
- (void)newsButtonDidPress:(id)sender {
    NSLog(@"头条");
    self.collectionView.contentOffset = CGPointMake(0, -65);
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    
}

- (void)amuseButtonDidPress:(id)sender {
     NSLog(@"娱乐");
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width, -55);
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
}

- (void)peButtonDidPress:(id)sender {
     NSLog(@"体育");
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width * 2, -55);
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_peButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
}

- (void)enocomyButtonDidPress:(id)sender {
     NSLog(@"经济");
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width * 3, -55);
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x <= self.view.frame.size.width * 0) {
        [_newsButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
        [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    } else if (scrollView.contentOffset.x <= self.view.frame.size.width * 1) {
        [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_amuseButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
        [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    } else if (scrollView.contentOffset.x <= self.view.frame.size.width * 2) {
        [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_peButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_enocomyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_peButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
        [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    } else {
        [_newsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_amuseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_peButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_enocomyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_newsButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_amuseButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_peButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [_enocomyButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
    }
    
}



#pragma mark - URL

- (void) getNewsData {
    [SANetWorkingTask request:kNewsGETURL parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = [dic valueForKey:@"T1348647909107"];
        for (NSDictionary *dic in array) {
            NewsModel *news = [[NewsModel alloc] initWithDictionary:dic];
            [self.dataSource addObject:news];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}

- (void)getAmuseData {
    [SANetWorkingTask request:kAmuseGETURL parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = [dic valueForKey:@"T1348648517839"];
        for (NSDictionary *dic in array) {
            NewsModel *amuseNewsModel = [[NewsModel alloc] initWithDictionary:dic];
            [self.amuseDataSource addObject:amuseNewsModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}

- (void)getPEData {
    [SANetWorkingTask request:kPEGETURL parmater:nil block:^(id result) {
        NSArray *array = [[NSArray alloc] initWithArray:[result valueForKey:@"T1348649079062"]];
        for (NSDictionary *dic in array) {
             NewsModel *peNewsModel = [[NewsModel alloc] initWithDictionary:dic];
            [self.peDataSource addObject:peNewsModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}
- (void)getEconomyData {
    [SANetWorkingTask request:kEconomyGETURL parmater:nil block:^(id result) {
        NSArray *array = [[NSArray alloc] initWithArray:[result valueForKey:@"T1348648756099"]];
        for (NSDictionary *dic in array) {
            NewsModel *economyNewsModel = [[NewsModel alloc] initWithDictionary:dic];
            [self.economyDataSource addObject:economyNewsModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}



#pragma mark - prive
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height - 113);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:@"NewsCollectionViewCellIdentifier"];
        
//        [_collectionView registerClass:[AmuseCollectionViewCell class] forCellWithReuseIdentifier:@"AmuseCollectionViewCellIdentifier"];
        
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        self.dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataSource;
}
-(NSMutableArray *)amuseDataSource {
    if (!_amuseDataSource) {
        self.amuseDataSource =[[NSMutableArray alloc] initWithCapacity:0];
    }
    return _amuseDataSource;
}
- (NSMutableArray *)peDataSource {
    if (!_peDataSource) {
        self.peDataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _peDataSource;
}
- (NSMutableArray *)economyDataSource {
    if (!_economyDataSource) {
        self.economyDataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _economyDataSource;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
        NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewsCollectionViewCellIdentifier" forIndexPath:indexPath];
     if (indexPath.item == 0) {
        if (self.dataSource.count > indexPath.item) {
            cell.arry = self.dataSource;
        }
        return cell;
    } else if (indexPath.item == 1) {
        if (self.amuseDataSource.count > indexPath.item) {
            cell.arry = self.amuseDataSource;
        }
        return cell;
    } else if (indexPath.item == 2 ) {
//        if (self.peDataSource.count > indexPath.item) {
            cell.arry = self.peDataSource;
            return cell;
//        }
    } else {
        if (self.economyDataSource.count > indexPath.item ) {
            cell.arry = self.economyDataSource;
            return cell;
        }
    }
    return nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
